// components/navbar/index.js
const App = getApp();

let api = require('../../utils/api.js');
Component({
  options: {
    addGlobalClass: true,
  },
  externalClasses: ['custom-class'],
  /**
   * 组件的属性列表
   */
  properties: {
    pageName: String,
    userLocationCity: String,
    showNav: {
      type: Boolean,
      value: false
    },
    showSave: {
      type: Boolean,
      value: false
    },
    bgColor: {
      type: String,
      value: '#FF8000'
    },
    iconColor: {
      type: String,
      value: '#FFFFFF'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    userLocationCity: '深圳市'
  },
  lifetimes: {
    attached: function () {
      this.setData({
        navHeight: App.globalData.navHeight,
        navTop: App.globalData.navTop
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 回退
    _navBack: function () {
      wx.navigateBack({
        delta: 1
      })
    },
    //回主页
    _toIndex: function () {
      wx.switchTab({
        url: '/pages/index/index'
      })
    },
    _toLocation: function () {
      wx.navigateTo({
        url: '/pages/user/location/location',
      })
    },
    /**
     * 修改定位
     */
    _saveLocation: function (e) {
      // console.log(App.globalData);
      var param = {
        "city": App.globalData.userLocationCity,
        "province": App.globalData.userLocationProvince
      }
      var token = wx.getStorageSync('TOKEN');
      api._put('/location/city', param, token).then(res => {
        if (res.code == 200) {
          wx.switchTab({
            url: '../../index/index',
            success: function (e) {
              var page = getCurrentPages().pop();
              if (page == undefined || page == null) return;
              page.onLoad();
            }
          })
        } else {
          wx.showToast({
            title: '网络忙，请稍后重试',
            icon: 'none',
          })
        }

      })
    }
  },

})